import { Injectable, Logger } from "@nestjs/common";
import { Cron } from "@nestjs/schedule";
import { HouseTrainingService } from "./house-training.service";
import { AppWsGateway } from "../gateway/ws.gateway";

@Injectable()
export class HouseTrainingSchedulerService {
  private readonly logger = new Logger(HouseTrainingSchedulerService.name);
  constructor(
    private readonly houseTrainingService: HouseTrainingService,
    private readonly wsGateway: AppWsGateway
  ) {}

  @Cron("*/1 * * * *")
  async tick() {
    const processed = await this.houseTrainingService.settleDueTasks(
      (characterId, payload) => {
        this.wsGateway.broadcastToUser(characterId, payload);
      }
    );
    if (processed > 0) {
      this.logger.log(`房屋训练任务结算完成：${processed} 条`);
    }
  }
}
